# -*- coding: UTF-8 -*-
__author__ = 'beibei'
'''
needinstall python nmap 包 以及 nmap程序
'''

import threading
import optparse
import nmap


def nmapScan(tgtHost, tgtPort):
    print (" start port" + str(tgtPort) + "\r\n")
    nmScan = nmap.PortScanner()
    result = nmScan.scan(tgtHost, str(tgtPort))
    state = result['scan'][tgtHost]['tcp'][int(tgtPort)]['state']
    print("[*]" + tgtHost + "tcp/" + str(tgtPort) + " " + state + "\r\n")


def main():
    parser = optparse.OptionParser('usage %prog -H <targethost> -p <target port>')
    parser.add_option('-H', dest='tgtHost', type='string', help='specify target host')
    parser.add_option('-p', dest='tgtPort', type='int', help='specify target port')
    (options, args) = parser.parse_args()
    tgtHost = options.tgtHost
    tgtPort = options.tgtPort
    args.append(tgtPort)
    if (tgtHost is None) or (tgtPort is None):
        print ('[-] You must specify a target host and port[s]!')
        print (parser.usage)
        exit(0)
    for tgport in args:
        # nmapScan(tgtHost,tgport)
        t = threading.Thread(target=nmapScan, args=(tgtHost, str(tgport)))
        t.start()


if __name__ == '__main__':
    main()
